package sync;

/**
 * 描述:单例模式
 *
 * @author
 * @created 2022/1/10 20:20
 */
public class Singleton {

}

/*懒汉式*/
class Singleton1{

    private static Singleton1 instance;

    private Singleton1(){

    }

    public static Singleton1 getInstance() {
        if (instance == null) {
            return new Singleton1();
        }
        return instance;
    }
}

/*饿汉式*/
class Singleton2{
    private static Singleton2 instance = new Singleton2();
    private Singleton2(){}

    public static Singleton2 getInstance() {
        return instance;
    }
}

/*双重校验式*/
class Singleton3{
    private volatile static Singleton3 instance;
    private Singleton3(){}
    public static Singleton3 getInstance() {
        if (instance == null) {
            synchronized (Singleton3.class) {
                if (instance == null) {
                    return new Singleton3();
                }
            }
        }
        return instance;
    }
}

/*静态内部类，延迟加载特性*/
/*枚举类*/
